package netty.bio;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * @program NettyLeaning
 * @description: 同步阻塞式IO处理 一请求一线程，线程可无限扩张
 *   - 弊端
 *      - 资源耗尽，安全隐患
 * @author: zhanglu
 * @create: 2022-01-16 18:48:00
 */
@Slf4j
public class TimeServerClient {

    private static final String IP = "127.0.0.1";
    private static final Integer PORT = 8080;

    @Test
    public void server() throws Exception {
        ServerSocket server = null;
        try {
            server = new ServerSocket(PORT);
            log.info("time server start port. " + PORT);
            Socket socket = null;
            while (true){
                socket = server.accept();
                Socket handlerSocket = socket;
                new Thread(() -> {
                    BufferedReader in = null;
                    PrintWriter out = null;
                    try {
                        in = new BufferedReader(new InputStreamReader(handlerSocket.getInputStream()));
                        out = new PrintWriter(handlerSocket.getOutputStream(), true);
                        String currentTime = null;
                        String body = null;
                        while (true){
                            body = in.readLine();
                            if(body == null){
                               break;
                            }
                            log.info("server receive : " + body);
                            currentTime = DateUtil.formatDateTime(new Date());
                            out.println("server return : " + currentTime);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if(in != null){
                            try {
                                in.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if(out != null){
                            out.close();
                        }
                        if(handlerSocket != null){
                            try {
                                handlerSocket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        }finally {
            if(server != null){
                log.info("server close");
                server.close();
            }
        }
    }

    @Test
    public void client() throws Exception{
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            socket = new Socket(IP, PORT);
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("hello server");
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String res = in.readLine();
            log.info(res);
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                out.close();
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
